// pages/product/product.js
const util = require("../../utils/util")
var app = getApp();
Page({

  data: {
    orderProduct: null,
    product: null,
    shoppingTrolleyProduct: -1,
  },

  insertformSubmit(e) {//新增
    //提交用户自定义好了的奶茶再录入数据库
    wx.request({
      url: util.BASE_URL + '/order/orderedproduct',
      method: "POST",
      data:{
        orderListId: app.globalData.orderList.id,
        productId: app.globalData.productIndex+1,
        temperature: e.detail.value.temperature,
        sweetness: e.detail.value.sweetness
      },
      success(res){
        console.log("奶茶定制提交完成！")
      }
    })
    wx.navigateBack({
      //返回上一层
      delta: 1,
    })
  },

  upDataformSubmit(e){//更改
     //提交用户更新好了的奶茶再录入数据库
     console.log(app.globalData.shoppingTrolley)
     wx.request({
      url: util.BASE_URL + '/order/updateOrderedProduct',
      method: "POST",
      data:{
        id: this.data.shoppingTrolleyProduct.id,
        orderListId: this.data.shoppingTrolleyProduct.orderListId,
        productId: this.data.shoppingTrolleyProduct.productId,
        temperature: e.detail.value.temperature,
        sweetness: e.detail.value.sweetness
      },
      success(res){
        console.log("奶茶定制修改完成！后端返回的数据为：",res.data)
      }
    })
    wx.navigateBack({
      //返回上一层
      delta: 1,
    })
  },

  formReset(e) {
    //重置
    console.log('form发生了reset事件，携带数据为：', e.detail.value)
  },

  delete(e){
    console.log(this.data.shoppingTrolleyProduct)
    wx.request({
      url: util.BASE_URL + '/order/deleteit',
      method: "POST",
      data:this.data.shoppingTrolleyProduct,
      success(res){
        console.log("奶茶定制删除完成！后端返回的数据为：",res.data)
      }
    })
    wx.navigateBack({
      //返回上一层
      delta: 1,
    })
  },

  onLoad: function (options) {
    //加载用户所选择的哪一个的奶茶信息
    if(app.globalData.shoppingTrolley != null ){
      
      this.setData({
        product: app.globalData.productList[app.globalData.shoppingTrolley.returnOrderedProducts[app.globalData.productIndex].productId-1], 
        shoppingTrolleyProduct: app.globalData.shoppingTrolley.returnOrderedProducts[app.globalData.productIndex]
        //从全局变量得到用户指定的产品信息
      })
    }else{
      this.setData({
        product: app.globalData.productList[app.globalData.productIndex],
        //从全局变量得到用户指定的产品信息
      })
    }
    
    console.log('用户所选择的已定制产品的产品信息',this.data.shoppingTrolleyProduct)
  }
})